#include "rclcpp/rclcpp.hpp"
#include "std_msgs/msg/string.hpp"
#include "custom_srv_msg/srv/custom_srv.hpp"

using std::placeholders::_1;
using namespace std::chrono_literals;

int main(int argc, char const *argv[])
{
    rclcpp::init(argc,argv);
    std::shared_ptr<rclcpp::Node> node = rclcpp::Node::make_shared("client_node");
    rclcpp::Client<custom_srv_msg::srv::CustomSrv>::SharedPtr cli_;
    cli_ = node->create_client<custom_srv_msg::srv::CustomSrv>("service_cli_demo");
    // rclcpp::spin(node);
    auto req = std::make_shared<custom_srv_msg::srv::CustomSrv::Request>();
    req->client = argv[1];
    RCLCPP_INFO(rclcpp::get_logger("rclcpp"),req->client.c_str());

    while (!cli_->wait_for_service(1s))
    {
        if(!rclcpp::ok())
        {
            RCLCPP_INFO(node->get_logger(),"wait connect server");
            return 0;
        }
        RCLCPP_INFO(rclcpp::get_logger("rclcpp"),"service shutdown,wait connect server");
    }

    auto result = cli_->async_send_request(req);
    if (rclcpp::spin_until_future_complete(node, result) ==
    rclcpp::FutureReturnCode::SUCCESS)
    {
        RCLCPP_INFO(rclcpp::get_logger("rclcpp"), result.get()->service.c_str());
    } else {
        RCLCPP_ERROR(rclcpp::get_logger("rclcpp"), "Failed to call service add_two_ints");
    }
    rclcpp::shutdown();
    return 0;
}
